package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketAd;
import com.windgo.bean.MarketAdExample;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.mapper.MarketAdMapper;
import com.windgo.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:ZhouXin
 * @Date:2022/10/12 21:06
 * @Description:
 **/
@Service
public class AdServiceImpl implements AdService{

    @Autowired
    MarketAdMapper marketAdMapper;

    @Override
    public CommonData<MarketAd> list(BasePageInfo info, String name, String content) {
        PageHelper.startPage(info.getPage(),info.getLimit());

        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if (!StringUtil.isEmpty(content)){
            criteria.andContentLike("%"+content+"%");
        }

        List<MarketAd> list = marketAdMapper.selectByExample(example);
        PageInfo<MarketAd> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    /**
     * 新增广告
     * @param marketAd
     * @return
     */
    @Override
    public MarketAd create(MarketAd marketAd) {
        marketAdMapper.insertSelective(marketAd);
        Integer id = marketAd.getId();
        MarketAd marketAd1 = marketAdMapper.selectByPrimaryKey(id);
        return marketAd1;
    }


    /**
     * 修改广告信息
     * @param marketAd
     * @return
     */
    @Override
    public MarketAd update(MarketAd marketAd) {
        marketAdMapper.updateByPrimaryKey(marketAd);
        Integer id = marketAd.getId();
        MarketAd marketAd1 = marketAdMapper.selectByPrimaryKey(id);
        return marketAd1;
    }


    /**
     * 根据id删除广告
     * @param id
     */
    @Override
    public void delete(Integer id) {
        marketAdMapper.deleteByPrimaryKey(id);
    }
}
